define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shopro/lucky/server/index' + location.search,
                    add_url: 'shopro/lucky/server/add',
                    edit_url: 'shopro/lucky/server/edit',
                    del_url: 'shopro/lucky/server/del',
                    multi_url: 'shopro/lucky/server/multi',
                    import_url: 'shopro/lucky/server/import',
                    table: 'shopro_lucky_server',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'goods.title', title: __('Goods.title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'goods.image', title: __('Goods.image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'lucky_rate', title: '成功比例(%)', operate: false},
                        //{field: 'number', title: __('Number'), operate: false},
                        {field: 'alr_number', title: __('Alr_number'), operate: false},
                        //{field: 'lucky_rate', title: __('Lucky_rate')},
                        //{field: 'ok_multiplier', title: __('Ok_multiplier')},
                        //{field: 'no_multiplier', title: __('No_multiplier')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'open_lucky_time', title: __('Open_lucky_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
                $('#c-end_time').on('click', function(e) {
                    var timeStr = $('#c-start_time').val();
                    var newTime = moment(timeStr, "YYYY-MM-DD HH:mm:ss").add(12, 'hours').format("YYYY-MM-DD HH:mm:ss");
                    var openTime = moment(timeStr, "YYYY-MM-DD HH:mm:ss").add(48, 'hours').format("YYYY-MM-DD HH:mm:ss");
                    $('#c-end_time').val(newTime);
                    $('#c-open_lucky_time').val(openTime);
                });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
